package com.wthink.hzgj.service.jt808

import com.wthink.hzgj.util.BCD
import com.wthink.hzgj.util.PreferencesUtils
import java.math.BigDecimal
import java.nio.ByteBuffer
import java.text.SimpleDateFormat
import java.util.*

/**
 * 行车计划请求
 */
class JT_0B07(
    var user: String, var time: String = SimpleDateFormat("yyMMdd").format(Date()) /*运营时间 */
) : JT808Message() {

    init {
        messageType = "0b07"
    }

    override fun writeToBytes(): ByteArray {
        val mix = PreferencesUtils.getLong("mix")
        val nowTime : BigDecimal = BigDecimal(mix).add(BigDecimal(System.currentTimeMillis()))
        time = SimpleDateFormat("yyMMdd").format(nowTime)
        val buf = ByteBuffer.allocate(4 + user.length)
        buf.put(BCD.ASCII_To_BCD(time.toByteArray(), time.length))
        buf.put(user.toByteArray())
        buf.put(0)

        buf.flip()
        val body = ByteArray(buf.remaining())
        buf.get(body)
        return body
    }
}